package sostats.core.query;

import java.util.ArrayList;
import java.util.List;

import com.google.common.collect.Lists;

public abstract class ConditionBuilder {
	private String attribute;
	public ConditionBuilder(String attribute){
		this.attribute = attribute;
	}
	public void setAttribute(String attribute){
		this.attribute = attribute;
	}
	
	public String getAttribute() {
		return attribute;
	}

	public abstract List<String> build(String[] params);
	public static class NumberBuilder extends ConditionBuilder{
		public NumberBuilder(String attribute){
			super(attribute);
		}
		@Override
		public List<String> build(String[] params) {
			if(params==null||params.length==0) return Lists.newArrayList();
			ArrayList<String> res = new  ArrayList<String>();
			String param = params[0];
			if(param!=null&&param.length()>0){
				StringBuilder condition = new StringBuilder();
				condition.append(getAttribute()).append(" > ").append(param);
				res.add(condition.toString());
			}
			if(params.length>1){
				param = params[1];
				if(param!=null&&param.length()>0){
					StringBuilder condition = new StringBuilder();
					condition.append(getAttribute()).append(" < ").append(param);
					res.add(condition.toString());
				}
			}
			return res;
		}
		
	}
	public static class DateBuilder extends ConditionBuilder{

		public DateBuilder(String attribute) {
			super(attribute);
		}
		@Override
		public List<String> build(String[] params) {
			if(params==null||params.length==0) return Lists.newArrayList();
			ArrayList<String> res = new  ArrayList<String>();
			String param = params[0];
			if(param!=null&&param.length()>0){
				StringBuilder condition = new StringBuilder();
				condition.append(getAttribute()).append(" > ").append(toDate(param));
				res.add(condition.toString());
			}
			if(params.length>1){
				param = params[1];
				if(param!=null&&param.length()>0){
					StringBuilder condition = new StringBuilder();
					condition.append(getAttribute()).append(" < ").append(toDate(param));
					res.add(condition.toString());
				}
			}
			return res;
		}
		private static String toDate(String date){
			return "to_date('"+date+" 00:00:00','yyyy-mm-dd hh24:mi:ss')";
		}
	}
}
